<?php
/*  
  Copyright (c) 2010-02 SongCMS 
  SongCMS All Rights Reserved. 
  Support:www.SongCMS.com
  QQ:544255858   19951025
  Author:Song  Version:3.17
  Date:2010-08-12 09:28:32
*/
class sdb extends db{
	private $result;
	public function WebConfig($Lang=1){
		$result = $this -> SelectSQL("SELECT * FROM `{$this -> dbprefix}webconfig` WHERE `WebLang` = {$Lang}",1);
		$result = array_shift($result);
		return empty($result) ? array() : $result;
	}
	public function column($params,&$SC){
		global $Lang,$isDynamicURL;
		extract($params);
		!is_numeric($Type) ? $Type = 0 : NULL; !empty($Colu) ? 0 : NULL;
		empty($BackVar) ? $BackVar = 'column' : NULL;
		$SQL = "SELECT * FROM `{$this -> dbprefix}column` WHERE ColumnType = {$Type} && ColumnLang = {$Lang} ";
		$SQL .= ($Colu!=0 && $Colu!=1) ? "&& `ColumnRela` LIKE '%" . $Colu . "%' && `ID` != '" . $Colu . "' " : ($Colu==1 ? "&& `ColumnLevel` = 1 " : NULL);
		$result = $this -> SelectSQL($SQL,1);
		$result = $this -> column_sort($result);
		if(!is_array($result)) $result = array();
		foreach ($result as $i => &$n){
			if($isDynamicURL == 1){
				$xLang = $Lang == 1 ? NULL : "/l{$Lang}";
				switch ($Type){
					case 0: $n['URL'] = "./?fpage{$xLang}/c{$n['ColumnRela']}"; break;
					case 1: $n['URL'] = "./?fnews{$xLang}/c{$n['ColumnRela']}"; break;
					case 2: $n['URL'] = "./?fproducts{$xLang}/c{$n['ColumnRela']}"; break;
					case 3: $n['URL'] = "./?fdownload{$xLang}/c{$n['ColumnRela']}"; break;
				}
			} else {
				switch ($Type){
					case 0: $n['URL'] = './html/page/' . $n['ID'] . '.html'; break;
					case 1: $n['URL'] = './html/news/' . $n['ID'] . '.html'; break;
					case 2: $n['URL'] = './html/products/' . $n['ID'] . '.html'; break;
					case 3: $n['URL'] = './html/download/' . $n['ID'] . '.html'; break;
				}				
			}
			$n['NextLevel'] = ($i+1) < count($result) ? $result[$i+1]['ColumnLevel'] : 0; 
		}
		$SC -> assign($BackVar,$result);
		unset($SQL,$params,$result);	
	}
	public function CidToName($params){
		extract($params);
		if (empty($Rela)) return;
		if (!is_numeric($level)) $level = 0;
		$Rela = explode('|',$Rela);
		if ($level == 0){
			$nRs = $this->SelectSQL("SELECT * FROM `{$this -> dbprefix}column` WHERE `ID` IN('". join('\',\'',$Rela) ."') ORDER BY `ColumnRela` ASC",1);
			foreach ($nRs as $i => $n){
				if(count($nRs)==($i+1)) $blank = NULL;
				$ArticleName .= '<a href="'.$this->CidToName_URL($n['ColumnType'],$n['ID']).'">'.$n['ColumnName'].'</a>'.$blank;
			}
		}else{
			$nRs = $this->SelectSQL("SELECT * FROM `{$this -> dbprefix}column` WHERE `ID` = '{$Rela[$level-1]}' ORDER BY `ColumnRela` ASC",1);
			$ArticleName = '<a href="'.$this->CidToName_URL($nRs[0]['ColumnType'],$nRs[0]['ID']).'">'.$nRs[0]['ColumnName'].'</a>';
		}
		unset($Rela,$blank,$nRs,$i,$n);
		return $ArticleName;
	}
	public function CidToName_URL($xType,$xCid){
		global $Lang,$isDynamicURL;
		if($isDynamicURL == 1){
			$xLang = $Lang == 1 ? NULL : "/l{$Lang}";
			switch ($xType){
				case 0: $xURL = "./?fpage{$xLang}/c{$xCid}"; break;
				case 1: $xURL = "./?fnews{$xLang}/c{$xCid}"; break;
				case 2: $xURL = "./?fproducts{$xLang}/c{$xCid}"; break;
				case 3: $xURL = "./?fdownload{$xLang}/c{$xCid}"; break;
			}
		} else {
			switch ($xType){
				case 0: $xURL = "./html/page/{$xCid}.html"; break;
				case 1: $xURL = "./html/news/{$xCid}.html"; break;
				case 2: $xURL = "./html/products/{$xCid}.html"; break;
				case 3: $xURL = "./html/download/{$xCid}.html"; break;
			}			
		}
		return $xURL;
	}
	public function sLink($params,&$SC){
		extract($params);
		global $Lang;
		if(!is_numeric($Type)) $Type = 2;
		if(!is_numeric($Start)) $Start = 0;
		if(!is_numeric($Total)) $Total = 10;
		if(empty($BackVar)) $BackVar = 'sLink';
		$SQL = "SELECT * FROM `{$this->dbprefix}link` WHERE `LinkIsAudit` = 1 && `LnikLang` = {$Lang} ";
		if($Type != 2) $SQL .= "&& `LinkType` = {$Type} ";
		$SQL .= "ORDER BY `LinkSort` DESC,`ID` ASC ";
		$SQL .= "LIMIT {$Start},{$Total}";
		$result = $this -> SelectSQL($SQL,1);
		$SC -> assign($BackVar,$result);
		unset($SQL,$params,$result);		
	}
	public function TemSQL($params,&$SC){
		extract($params);
		if(empty($SQL)) return false;
		if(empty($BackVar)) $BackVar = 'MyArray';
		$SQL = str_replace('{#dbprefix}',$this -> dbprefix,$SQL);
		$result = $this -> SelectSQL($SQL,1);
		if(count($result) == 1) $result = array_shift($result);
		$SC -> assign($BackVar,$result);
	}
	public function Related($ID,$Table){
		global $SC,$isDynamicURL,$Lang;
		$sLang = $Table=='news' ? 'NewsLang' : ($Table=='product' ? 'ProductLang' : 'ArticleLang');
		$SQL = "SELECT * FROM `{$this->dbprefix}{$Table}` WHERE `{$sLang}` = {$Lang} && `ID` < {$ID} ORDER BY `ID` DESC LIMIT 0,1";
		$Next = $this -> SelectSQL($SQL,1);
		$SQL = "SELECT * FROM `{$this->dbprefix}{$Table}` WHERE `{$sLang}` = {$Lang} && `ID` > {$ID} ORDER BY `ID` ASC LIMIT 0,1";
		$Previous = $this -> SelectSQL($SQL,1);
		@$Previous = $Previous[0]; $Next = $Next[0];
		if($isDynamicURL == 1){
			$DDIR = $Table=='article' ? 'page' : ($Table=='news' ? 'new' : 'product');
			$Previous['URL'] = "./?f{$DDIR}/l{$Lang}/i{$Previous['ID']}";
			$Next['URL'] = "./?f{$DDIR}/l{$Lang}/i{$Next['ID']}";
		} else {
			switch($Table){
				case 'news':
					$Previous['URL'] = './html/news/' . date("Ym", strtotime($Previous['NewsTime'])) . '/' . $Previous['ID'] . '.html';
					$Next['URL'] = './html/news/' . date("Ym", strtotime($Next['NewsTime'])) . '/' . $Next['ID'] . '.html';
					break;
				case 'product':
					$Previous['URL'] = './html/products/' . date("Ym", strtotime($Previous['ProductTime'])) . '/' . $Previous['ID'] . '.html';
					$Next['URL'] = './html/products/' . date("Ym", strtotime($Next['ProductTime'])) . '/' . $Next['ID'] . '.html';
					break;
				default:
					$Previous['URL'] = './html/page/' . date("Ym", strtotime($Previous['ArticleTime'])) . '/' . $Previous['ID'] . '.html';
					$Next['URL'] = './html/page/' . date("Ym", strtotime($Next['ArticleTime'])) . '/' . $Next['ID'] . '.html';
			}		
		}
		$SC->assign('Previous',$Previous);	
		$SC->assign('Next',$Next);	
		unset($SQL,$sLang,$DDIR);
	}
	public function column_sort($data){
		$max_sort = 0;
		foreach($data as $i => $n){
			if($n['ColumnLevel'] > $max_sort) $max_sort = $n['ColumnLevel'];
		}
		foreach($data as $i => $n){
			for($x=1; $x<=$max_sort; $x++){
				if($n['ColumnLevel'] == $x){
					${'rela_'.$x}[] = $n;
				}
			}
		}
		for($i=1; $i<=$max_sort; $i++){	
			if(is_array(${'rela_'.$i})){
				foreach (${'rela_'.$i} as $o => $p) {
					${'sort_'.$i}[$o] = $p['ColumnSort'];
				}
				array_multisort(${'sort_'.$i},SORT_ASC,${'rela_'.$i});
			}
		}
		if(is_array($rela_1)){
			foreach($rela_1 as $i => $n){
				$all_column_1[] = $n;
				if(!is_array($rela_2)) break; 
				foreach($rela_2 as $x => $y){
					if(stristr($y['ColumnRela'],$n['ColumnRela'])) $all_column_1[] = $y;
				}
			}
		}
		if(empty($rela_1)) $all_column_1 = $rela_2; 
		for($i=2; $i<$max_sort; $i++){
			if(empty(${'rela_'.$i})) ${'all_column_'.$i} = ${'rela_'.($i+1)}; 
			if(is_array(${'all_column_'.($i-1)})){
				foreach(${'all_column_'.($i-1)} as $o => $p){
					${'all_column_'.$i}[] = $p;
					if($p['ColumnLevel'] == $i){
						foreach(${'rela_'.($i+1)} as $e => $r){
							if(stristr($r['ColumnRela'],$p['ColumnRela'])) ${'all_column_'.$i}[] = $r;
						}
					}
				}
			}
		}
		$all_column = ${'all_column_' . ($max_sort-1)}; 
		if(empty($all_column) || $max_sort == 1) $all_column = $rela_1; 
		return $all_column;
	}
	public function html2txt($params){
		extract($params);
		if(empty($word)) return false;
		if(!is_numeric($length)) $length = 0;
		if(!is_numeric($illegal)) $illegal = 0;
		if(!isset($suffix)) $suffix = ' ...';
		$search = array(
			'@<script[^>]*?>.*?</script>@si', 
			'@<style[^>]*?>.*?</style>@si', 
			'@<[\\/\\!]*?[^<>]*?>@si', 
			'@<![\\s\\S]*?--[ \\t\\n\\r]*>@', 
			"'([\r\n])[\s]+'", 
			"'&(quot|#34);'i", 
			"'&(amp|#38);'i",
			"'&(lt|#60);'i",
			"'&(gt|#62);'i",
			"'&(nbsp|#160);'i",
			"'&(iexcl|#161);'i",
			"'&(cent|#162);'i",
			"'&(pound|#163);'i",
			"'&(copy|#169);'i"
		);
		if ($illegal == 1) $search[] = '@[\'\"]@';
		$text = preg_replace($search, '', $word);
		if($length > 0) $text = FSubstr($text,0,$length,$suffix,'utf-8');
		return $text;
	}	
	public function survey($params){
		global $Lang,$SC;
		extract($params);
		if(empty($ID) || !is_numeric($ID) || $ID==0) $ID = -1;
		if(empty($isReturn) || !is_numeric($isReturn)) $isReturn = 0;
		if($ID > 0){
			$SQL = "SELECT * FROM `{$this->dbprefix}survey` WHERE ID = '{$ID}' LIMIT 0,1;";
		} else {
			$ID = ($ID * -1) - 1;
			$SQL = "SELECT * FROM `{$this->dbprefix}survey` WHERE `SurveyLang`='{$Lang}' ORDER BY `ID` DESC LIMIT {$ID},1";
		}
		if($result = $this -> SelectSQL($SQL,1)){
			$result = array_shift($result);
			$result['SurveyOptions'] = explode('|',$result['SurveyOptions']); 
			$result['SurveyCount'] = explode('|',$result['SurveyCount']); 
			$result['Count'] = count($result['SurveyOptions']); 
			$result['URL'] = $Lang==1 ? "./?fsurvey/i{$result['ID']}" : "./?fsurvey/l{$Lang}/i{$result['ID']}"; 
			$Total = array_sum($result['SurveyCount']); 
			$result['Status'] = strtotime($result['SurveyStart']) < time() && strtotime($result['SurveyEnd']) > time() ? 1 : 0; 
			for($i=0; $i<$result['Count']; $i++){
				$result['InputHTML'][$i] = $result['SurveyMany']==0 ? "<input name=\"survey_radio\" type=\"radio\" value=\"{$i}\" />" : "<input name=\"survey_checkbox[]\" type=\"checkbox\" value=\"{$i}\" />";
				$result['Percent'][$i] = @round($result['SurveyCount'][$i]/$Total*100,1);
				$result['iWidth'][$i] = round($result['Percent'][$i] * 3,0);
			}
		} else {
			$result = array();
		}
		if($isReturn == 0){
			$SC->assign('survey',$result);
		} else {
			return $result;
		}
	}
	public function CommentTotal($type,$id){
		global $WebCommentShow; 
		$SQL = "SELECT COUNT(`ID`) FROM `{$this->dbprefix}comment` WHERE `CommentType` = '{$type}' && `CommentID` = '{$id}'";
		if($WebCommentShow == 1) $SQL .= " && `CommentAudit` = '1' "; 
		$result = $this -> SelectSQL($SQL,0);
		return $result[0][0];
	}
}
?>